package com.liao.work.ui.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.liao.work.R;
import com.liao.work.util.SetActionBarTitle;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private EditText et_password1;
    private RadioButton rb_boy;
    private RadioButton rb_girl;
    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private CheckBox cb4;
    private Button btn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        SetActionBarTitle.setTitle(getSupportActionBar(), false, "注册");
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password1 = (EditText) findViewById(R.id.et_password1);
        rb_boy = (RadioButton) findViewById(R.id.rb_boy);
        rb_girl = (RadioButton) findViewById(R.id.rb_girl);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        btn_register = (Button) findViewById(R.id.btn_register);

        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String username = et_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            et_username.setError("用户名不能为空");
            return;
        }

        String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            et_password.setError("密码不能为空");
            return;
        }

        String password1 = et_password1.getText().toString().trim();
        if (TextUtils.isEmpty(password1)) {
            et_password1.setError("不能为空");
            return;
        }

        if (!password.equals(password1)) {
            Toast.makeText(this, "两次密码不一样", Toast.LENGTH_SHORT).show();
            et_password1.setText("");
            return;
        }
        String sex = rb_boy.isChecked() ? "男" : "女";
        String hobby = "";
        if (cb1.isChecked()) {
            hobby += " " + cb1.getText();
        }
        if (cb2.isChecked()) {
            hobby += " " + cb2.getText();
        }
        if (cb3.isChecked()) {
            hobby += " " + cb3.getText();
        }
        if (cb4.isChecked()) {
            hobby += " " + cb4.getText();
        }
        Intent intent = new Intent(this, RegisterDetailActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("password", password);
        intent.putExtra("sex", sex);
        intent.putExtra("hobby", hobby);
        startActivity(intent);
    }
}
